<template>
  <div class="container">
    <div class="app-container">
      <!-- data：指定el-tree要渲染的树形数据 -->
      <!-- default-expand-all：默认展开所有节点 -->
      <!-- props：可以修改el-tree组件的默认读取字段 -->
      <el-tree
        :data="list"
        :props="defaultProps"
        default-expand-all
        :expand-on-click-node="false"
      >
        <!-- el-tree组件的插槽内容 -->
        <!-- 插槽内容就是树形结构中一个节点的内容 -->
        <template #default="{ data }">
          <!-- 使用作用域插槽，获取el-tree组件内部传递出来的data -->
          <!-- data：树形数据中每个节点的数据 -->
          <el-row
            style="width: 100%"
            type="flex"
            justify="space-between"
            align="middle"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event就是子传父时，子组件传递给父组件的数据 -->
              <el-dropdown @command="treeOperate($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- 添加子部门弹窗 -->
    <department-dialog
      ref="departDialogRef"
      :showDialog="showDialog"
      @closeDialog="closeDialog"
      :currentId="currentId"
      @getDepartmentList="getDepartmentList"
    ></department-dialog>
    <!-- $event的用法 -->
    <!-- <EventUsage ref="eventRef" @getCount="getCount($event, 'zs')"></EventUsage>
    <button @click="$refs.eventRef.sayHi()">
      调用EventUsage组件的sayHi方法
    </button> -->
  </div>
</template>
<script>
import { delDepartment, getDepartmentList } from "@/api/department";
import { listToTree } from "@/utils/index";
import DepartmentDialog from "./components/department-dialog.vue";
import EventUsage from "@/components/test/$eventUsage.vue";
export default {
  name: "Department",
  components: {
    DepartmentDialog,
    EventUsage,
  },
  data() {
    return {
      list: [],
      defaultProps: {
        label: "name",
        // children: "child",
      },
      // 控制弹窗显示与隐藏的布尔值
      showDialog: false,
      // 当前操作的节点id
      currentId: 0,
    };
  },
  created() {
    // 获取组织架构列表数据
    this.getDepartmentList();
  },
  methods: {
    getCount(count, name) {
      // console.log(count, name);
    },
    // 获取组织架构列表数据
    async getDepartmentList() {
      const res = await getDepartmentList();
      this.list = listToTree(res.data, 0);
    },
    treeOperate(type, id) {
      console.log(id);
      this.currentId = id;
      if (type === "add") {
        // 添加子部门操作
        // 显示弹窗
        this.showDialog = true;
      } else if (type === "edit") {
        // 编辑操作
        // 显示弹窗
        this.showDialog = true;
        // 组件渲染时异步的，在$nextTick的回调函数里面，组件才渲染完成
        this.$nextTick(() => {
          // 调用弹窗子组件获取部门详情的方法
          this.$refs.departDialogRef.getDepartmentDetail();
        });
      } else {
        // 删除操作
        // 询问用户，是否删除
        this.$confirm("此操作将永久删除该部门, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            // 用户点击确定
            // 调用删除接口
            await delDepartment(this.currentId);
            // 提示用户
            this.$message.success("删除成功");
            // 刷新列表数据
            this.getDepartmentList();
          })
          .catch(() => {
            // 用户点击取消
          });
      }
    },
    closeDialog(value) {
      // 修改showDialog的值
      this.showDialog = value;
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  display: inline-block;
  width: 50px;
  margin-right: 10px;
}
</style>
